1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use crate::sketchbook::ids::{
    DatasetId, DynPropertyId, ObservationId, StatPropertyId, UninterpretedFnId, VarId,
};
use crate::sketchbook::model::{Essentiality, Monotonicity};
use crate::sketchbook::properties::dynamic_props::are_same_dyn_variant;
use crate::sketchbook::properties::static_props::{are_same_stat_variant, StatPropertyType};
use crate::sketchbook::properties::{
    DynPropIterator, DynProperty, PropertyManager, StatPropIterator, StatProperty,
};
use crate::sketchbook::utils::assert_ids_unique;
use std::collections::HashMap;
use std::str::FromStr;

/// Creating new instances of `PropertyManager`.
impl PropertyManager {
    /// Instantiate `PropertyManager` with empty sets of properties.
    pub fn new_empty() -> PropertyManager {
        PropertyManager {
            dyn_properties: HashMap::new(),
            stat_properties: HashMap::new(),
        }
    }

    /// Instantiate `PropertyManager` with dynamic and static properties given as a list
    /// of ID-property pairs.
    pub fn new_from_properties(
        dyn_properties: Vec<(&str, DynProperty)>,
        stat_properties: Vec<(&str, StatProperty)>,
    ) -> Result<PropertyManager, String> {
        let mut manager = PropertyManager::new_empty();

        let dyn_prop_ids = dyn_properties.iter().map(|pair| pair.0).collect();
        assert_ids_unique(&dyn_prop_ids)?;

        let stat_prop_ids = stat_properties.iter().map(|pair| pair.0).collect();
        assert_ids_unique(&stat_prop_ids)?;

        for (id, prop) in dyn_properties {
            manager.dyn_properties.insert(DynPropertyId::new(id)?, prop);
        }
        for (id, prop) in stat_properties {
            manager
                .stat_properties
                .insert(StatPropertyId::new(id)?, prop);
        }
        Ok(manager)
    }
}

/// Editing `PropertyManager`.
impl PropertyManager {
    /// Add pre-generated dynamic property.
    pub fn add_dynamic(&mut self, id: DynPropertyId, prop: DynProperty) -> Result<(), String> {
        self.assert_no_dynamic(&id)?;
        self.dyn_properties.insert(id, prop);
        Ok(())
    }

    /// Add pre-generated dynamic property with id given by str.
    pub fn add_dynamic_by_str(&mut self, id: &str, prop: DynProperty) -> Result<(), String> {
        let id = DynPropertyId::new(id)?;
        self.add_dynamic(id, prop)
    }

    /// Add pre-generated static property.
    pub fn add_static(&mut self, id: StatPropertyId, prop: StatProperty) -> Result<(), String> {
        self.assert_no_static(&id)?;
        self.stat_properties.insert(id, prop);
        Ok(())
    }

    /// Add pre-generated static property with id given by str.
    pub fn add_static_by_str(&mut self, id: &str, prop: StatProperty) -> Result<(), String> {
        let id = StatPropertyId::new(id)?;
        self.add_static(id, prop)
    }

    /// Set name for given dynamic property.
    pub fn set_dyn_name(&mut self, id: &DynPropertyId, new_name: &str) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.set_name(new_name)
    }

    /// Set name for given static property.
    pub fn set_stat_name(&mut self, id: &StatPropertyId, new_name: &str) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_name(new_name)
    }

    /// Update dynamic property's sub-field `dataset` where applicable.
    /// If not applicable, return `Err`.
    pub fn set_dyn_dataset(
        &mut self,
        id: &DynPropertyId,
        new_dataset: DatasetId,
    ) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.set_dataset(new_dataset)
    }

    /// Update dynamic property's sub-field `observation` where applicable.
    /// If not applicable, return `Err`.
    pub fn set_dyn_observation(
        &mut self,
        id: &DynPropertyId,
        new_obs: ObservationId,
    ) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.set_observation(new_obs)
    }

    /// Update generic dynamic property's formula.
    /// If not applicable (different variant), return `Err`.
    pub fn set_dyn_formula(&mut self, id: &DynPropertyId, new_formula: &str) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.set_formula(new_formula)
    }

    /// Update dynamic property's sub-field `observation` to None where applicable.
    /// If not applicable, return `Err`.
    pub fn set_dyn_none_observation(&mut self, id: &DynPropertyId) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.remove_observation()
    }

    /// Update dynamic property's sub-fields, if the property is of `AttractorCount` variant.
    /// If not applicable, return `Err`.
    pub fn set_dyn_attr_count(
        &mut self,
        id: &DynPropertyId,
        minimal: usize,
        maximal: usize,
    ) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.set_attr_count(minimal, maximal)
    }

    /// Update dynamic property's sub-fields, if the property is of `ExistsTrapSpace` variant.
    /// If not applicable, return `Err`.
    pub fn set_dyn_trap_space_details(
        &mut self,
        id: &DynPropertyId,
        is_minimal: bool,
        non_percolable: bool,
    ) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        let prop = self.dyn_properties.get_mut(id).unwrap();
        prop.set_trap_space_details(is_minimal, non_percolable)
    }

    /// Update generic static property's formula.
    /// If not applicable (different variant), return `Err`.
    pub fn set_stat_formula(
        &mut self,
        id: &StatPropertyId,
        new_formula: &str,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_formula(new_formula)
    }

    /// Update static property's sub-field for input variable (of an update fn), where applicable.
    /// If not applicable, return `Err`.
    pub fn set_stat_input_var(
        &mut self,
        id: &StatPropertyId,
        new_var: VarId,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_input_var(new_var)
    }

    /// Update static property's sub-field for index of input (of an uninterpreted fn),
    /// where applicable. If not applicable, return `Err`.
    pub fn set_stat_input_index(
        &mut self,
        id: &StatPropertyId,
        new_idx: usize,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_input_index(new_idx)
    }

    /// Update static property's sub-field for target uninterpreted fn, where applicable.
    /// If not applicable, return `Err`.
    pub fn set_stat_target_fn(
        &mut self,
        id: &StatPropertyId,
        new_target: UninterpretedFnId,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_target_fn(new_target)
    }

    /// Update static property's sub-field for target variable, where applicable.
    /// If not applicable, return `Err`.
    pub fn set_stat_target_var(
        &mut self,
        id: &StatPropertyId,
        new_target: VarId,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_target_var(new_target)
    }

    /// Update static property's sub-field for monotonicity, where applicable.
    /// If not applicable, return `Err`.
    pub fn set_stat_monotonicity(
        &mut self,
        id: &StatPropertyId,
        monotonicity: Monotonicity,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_monotonicity(monotonicity)
    }

    /// Update static property's sub-field for essentiality, where applicable.
    /// If not applicable, return `Err`.
    pub fn set_stat_essentiality(
        &mut self,
        id: &StatPropertyId,
        essentiality: Essentiality,
    ) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_essentiality(essentiality)
    }

    /// Update static property's sub-field for context, where applicable.
    /// If not applicable, return `Err`.
    pub fn set_stat_context(&mut self, id: &StatPropertyId, context: String) -> Result<(), String> {
        self.assert_valid_static(id)?;
        let prop = self.stat_properties.get_mut(id).unwrap();
        prop.set_context(context)
    }

    /// Swap content of a dynamic property with given `id`. The ID must be valid identifier.
    /// The variant of the prop. must stay the same (i.e., we only change attributes, not variant).
    pub fn swap_dyn_content(
        &mut self,
        id: &DynPropertyId,
        new_content: DynProperty,
    ) -> Result<(), String> {
        let orig_content = self.get_dyn_prop(id)?;
        if !are_same_dyn_variant(new_content.get_prop_data(), orig_content.get_prop_data()) {
            return Err("Variant of the dynamic property cannot change.".to_string());
        }
        self.dyn_properties.insert(id.clone(), new_content);
        Ok(())
    }

    /// Swap content of a dynamic property with given `id`. The ID must be valid identifier.
    /// The variant of the prop. must stay the same (i.e., we only change attributes, not variant).
    pub fn swap_dyn_content_by_str(
        &mut self,
        id: &str,
        new_content: DynProperty,
    ) -> Result<(), String> {
        let prop_id = DynPropertyId::new(id)?;
        self.swap_dyn_content(&prop_id, new_content)
    }

    /// Swap content of a static property with given `id`. The ID must be valid identifier.
    /// The variant of the prop. must stay the same (i.e., we only change attributes, not variant).
    pub fn swap_stat_content(
        &mut self,
        id: &StatPropertyId,
        new_content: StatProperty,
    ) -> Result<(), String> {
        let orig_content = self.get_stat_prop(id)?;
        if !are_same_stat_variant(new_content.get_prop_data(), orig_content.get_prop_data()) {
            return Err("Variant of the static property cannot change.".to_string());
        }
        self.stat_properties.insert(id.clone(), new_content);
        Ok(())
    }

    /// Swap content of a static property with given `id`. The ID must be valid identifier.
    /// The variant of the prop. must stay the same (i.e., we only change attributes, not variant).
    pub fn swap_stat_content_by_str(
        &mut self,
        id: &str,
        new_content: StatProperty,
    ) -> Result<(), String> {
        let prop_id = StatPropertyId::new(id)?;
        self.swap_stat_content(&prop_id, new_content)
    }

    /// Change ID of a dynamic property.
    pub fn set_dyn_id(
        &mut self,
        original_id: &DynPropertyId,
        new_id: DynPropertyId,
    ) -> Result<(), String> {
        self.assert_valid_dynamic(original_id)?;
        self.assert_no_dynamic(&new_id)?;

        if let Some(property) = self.dyn_properties.remove(original_id) {
            self.dyn_properties.insert(new_id.clone(), property);
        } else {
            panic!("Error when modifying dyn property's id in the property map.");
        }
        Ok(())
    }

    /// Change ID of a dynamic property, with IDs given as string slices.
    pub fn set_dyn_id_by_str(&mut self, original_id: &str, new_id: &str) -> Result<(), String> {
        let original_id = DynPropertyId::new(original_id)?;
        let new_id = DynPropertyId::new(new_id)?;
        self.set_dyn_id(&original_id, new_id)
    }

    /// Change ID of a static property.
    pub fn set_stat_id(
        &mut self,
        original_id: &StatPropertyId,
        new_id: StatPropertyId,
    ) -> Result<(), String> {
        self.assert_valid_static(original_id)?;
        self.assert_no_static(&new_id)?;

        if let Some(property) = self.stat_properties.remove(original_id) {
            self.stat_properties.insert(new_id.clone(), property);
        } else {
            panic!("Error when modifying stat property's id in the property map.");
        }
        Ok(())
    }

    /// Change ID of a static property, with IDs given as string slices.
    pub fn set_stat_id_by_str(&mut self, original_id: &str, new_id: &str) -> Result<(), String> {
        let original_id = StatPropertyId::new(original_id)?;
        let new_id = StatPropertyId::new(new_id)?;
        self.set_stat_id(&original_id, new_id)
    }

    /// Remove dynamic property.
    pub fn remove_dynamic(&mut self, id: &DynPropertyId) -> Result<(), String> {
        self.assert_valid_dynamic(id)?;
        self.dyn_properties.remove(id).unwrap();
        Ok(())
    }

    /// Remove static property.
    pub fn remove_static(&mut self, id: &StatPropertyId) -> Result<(), String> {
        self.assert_valid_static(id)?;
        self.stat_properties.remove(id).unwrap();
        Ok(())
    }

    /// Go through all static properties that are automatically generated from the regulation
    /// graph and make their IDs consistent with the variables they reference.
    ///
    /// This is useful after we change the variable's ID, e.g., to ensure that monotonicity
    /// properties still have IDs like `monotonicity_REGULATOR_TARGET`.
    pub fn make_generated_reg_prop_ids_consistent(&mut self) {
        // list of old-new IDs that must be changed
        let mut id_change_list: Vec<(StatPropertyId, StatPropertyId)> = Vec::new();
        for (prop_id, prop) in self.stat_properties.iter() {
            match prop.get_prop_data() {
                StatPropertyType::RegulationEssential(p) => {
                    // this template always has both fields, we can unwrap
                    let expected_id = StatProperty::get_essentiality_prop_id(
                        p.input.as_ref().unwrap(),
                        p.target.as_ref().unwrap(),
                    );
                    if prop_id != &expected_id {
                        id_change_list.push((prop_id.clone(), expected_id.clone()));
                    }
                }
                StatPropertyType::RegulationMonotonic(p) => {
                    // this template always has both fields, we can unwrap
                    let expected_id = StatProperty::get_monotonicity_prop_id(
                        p.input.as_ref().unwrap(),
                        p.target.as_ref().unwrap(),
                    );
                    if prop_id != &expected_id {
                        id_change_list.push((prop_id.clone(), expected_id.clone()));
                    }
                }
                _ => {}
            }
        }
        for (current_id, new_id) in id_change_list {
            self.set_stat_id(&current_id, new_id).unwrap();
        }
    }
}

/// Internal assertion utilities.
impl PropertyManager {
    /// **(internal)** Utility method to ensure there is no dynamic property with given ID yet.
    fn assert_no_dynamic(&self, id: &DynPropertyId) -> Result<(), String> {
        if self.is_valid_dyn_property_id(id) {
            Err(format!("Dynamic property with id {id} already exists."))
        } else {
            Ok(())
        }
    }

    /// **(internal)** Utility method to ensure there is a dynamic property with given ID.
    fn assert_valid_dynamic(&self, id: &DynPropertyId) -> Result<(), String> {
        if self.is_valid_dyn_property_id(id) {
            Ok(())
        } else {
            Err(format!("Dynamic property with id {id} does not exist."))
        }
    }

    /// **(internal)** Utility method to ensure there is no static property with given ID yet.
    fn assert_no_static(&self, id: &StatPropertyId) -> Result<(), String> {
        if self.is_valid_stat_property_id(id) {
            Err(format!("Static property with id {id} already exists."))
        } else {
            Ok(())
        }
    }

    /// **(internal)** Utility method to ensure there is a static property with given ID.
    fn assert_valid_static(&self, id: &StatPropertyId) -> Result<(), String> {
        if self.is_valid_stat_property_id(id) {
            Ok(())
        } else {
            Err(format!("Static property with id {id} does not exist."))
        }
    }
}

/// Observing the `PropertyManager`.
impl PropertyManager {
    /// The number of dynamic properties in this `PropertyManager`.
    pub fn num_dyn_properties(&self) -> usize {
        self.dyn_properties.len()
    }

    /// The number of static properties in this `PropertyManager`.
    pub fn num_stat_properties(&self) -> usize {
        self.stat_properties.len()
    }

    /// Check if there is a dynamic property with given Id.
    pub fn is_valid_dyn_property_id(&self, id: &DynPropertyId) -> bool {
        self.dyn_properties.contains_key(id)
    }

    /// Check if there is a static property with given Id.
    pub fn is_valid_stat_property_id(&self, id: &StatPropertyId) -> bool {
        self.stat_properties.contains_key(id)
    }

    /// Return an iterator over all dynamic properties of this model.
    pub fn dyn_props(&self) -> DynPropIterator {
        self.dyn_properties.iter()
    }

    /// Return an iterator over all dynamic properties of this model.
    pub fn stat_props(&self) -> StatPropIterator {
        self.stat_properties.iter()
    }

    /// Return a valid dynamic property's `DynPropertyId` corresponding to the given str `id`.
    ///
    /// Return `Err` if such property does not exist (and the ID is invalid).
    pub fn get_dyn_prop_id(&self, id: &str) -> Result<DynPropertyId, String> {
        let property_id = DynPropertyId::from_str(id)?;
        if self.is_valid_dyn_property_id(&property_id) {
            return Ok(property_id);
        }
        Err(format!("Dynamic property with ID {id} does not exist."))
    }

    /// Return a `DynProperty` corresponding to a given `DynPropertyId`.
    ///
    /// Return `Err` if such dynamic property does not exist (the ID is invalid in this context).
    pub fn get_dyn_prop(&self, id: &DynPropertyId) -> Result<&DynProperty, String> {
        let dyn_prop = self
            .dyn_properties
            .get(id)
            .ok_or(format!("Dynamic property with ID {id} does not exist."))?;
        Ok(dyn_prop)
    }

    /// Return a valid static property's `StatPropertyId` corresponding to the given str `id`.
    ///
    /// Return `Err` if such property does not exist (and the ID is invalid).
    pub fn get_stat_prop_id(&self, id: &str) -> Result<StatPropertyId, String> {
        let property_id = StatPropertyId::from_str(id)?;
        if self.is_valid_stat_property_id(&property_id) {
            return Ok(property_id);
        }
        Err(format!("Static property with ID {id} does not exist."))
    }

    /// Return a `StatProperty` corresponding to a given `StatPropertyId`.
    ///
    /// Return `Err` if such static property does not exist (the ID is invalid in this context).
    pub fn get_stat_prop(&self, id: &StatPropertyId) -> Result<&StatProperty, String> {
        let stat_prop = self
            .stat_properties
            .get(id)
            .ok_or(format!("Static property with ID {id} does not exist."))?;
        Ok(stat_prop)
    }
}